using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class LoaiKhachDAO
    {
        private LoaiKhachDTO[] ConvertDTtoArr(DataTable dt)
        {
            int n = dt.Rows.Count;
            LoaiKhachDTO[] LK = new LoaiKhachDTO[n];
            for (int i = 0; i < n; i++)
            {
                LK[i] = new LoaiKhachDTO();
                LK[i].Id_LK = Convert.ToInt32(dt.Rows[i]["ID_LK"].ToString());
                LK[i].MaLK = dt.Rows[i]["MaLK"].ToString();
                LK[i].TenLK = dt.Rows[i]["TenLK"].ToString();
                LK[i].HeSo = float.Parse(dt.Rows[i]["HeSo"].ToString());
            }
            return LK;
        }
        public LoaiKhachDTO[] LayDSLoaiKhach()
        {
            LoaiKhachDTO[] LK = new LoaiKhachDTO[100];
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                con = dp.ConnectionData();
                con.Open();
                DataTable dt = new DataTable();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_layDSLoaiKhach";
                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                LK = ConvertDTtoArr(dt);
                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return LK;
        }
        public void ThemLoaiKhach(string MaLK,string TenLK, Double HeSo)
        {
            LoaiKhachDTO lkDTO = new LoaiKhachDTO();
            lkDTO.MaLK = MaLK;
            lkDTO.TenLK = TenLK;
            lkDTO.HeSo = HeSo;
            ThemLoaiKhach(lkDTO);
        }

        private void ThemLoaiKhach(LoaiKhachDTO lk)
        {
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                con = dp.ConnectionData();
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_ThemLoaiKhach";

                cmd.Parameters.Add("@MaLK", SqlDbType.Char);
                cmd.Parameters.Add("@TenLK", SqlDbType.NVarChar);
                cmd.Parameters.Add("@HeSo", SqlDbType.Float);

                cmd.Parameters["@MaLK"].Value = lk.MaLK;
                cmd.Parameters["@TenLK"].Value = lk.TenLK;
                cmd.Parameters["@HeSo"].Value = lk.HeSo;

                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        public void CapNhatLoaiKhach(LoaiKhachDTO lk)
        {
            try
            {
                SqlConnection con = new SqlConnection();
                DataProvider dp = new DataProvider();
                con = dp.ConnectionData();
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "sp_CapNhatLoaiKhach";

                cmd.Parameters.Add("@ID_LK", SqlDbType.Int);
                cmd.Parameters.Add("@MaLK", SqlDbType.Char);
                cmd.Parameters.Add("@TenLK", SqlDbType.NVarChar);
                cmd.Parameters.Add("@HeSo", SqlDbType.Float);

                cmd.Parameters["@ID_LK"].Value = lk.Id_LK;
                cmd.Parameters["@MaLK"].Value = lk.MaLK;
                cmd.Parameters["@TenLK"].Value = lk.TenLK;
                cmd.Parameters["@HeSo"].Value = lk.HeSo;

                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
    }
}
